<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { HomeFilled, DataAnalysis, User, Calendar, ChatDotRound, Bell, FirstAidKit, PieChart, Avatar, ArrowRight, ArrowLeft } from '@element-plus/icons-vue'
const router = useRouter()

// 控制侧边栏收缩状态
const isCollapse = ref(false)

// 切换侧边栏收缩状态
const toggleCollapse = () => {
  isCollapse.value = !isCollapse.value
  // 向父组件发送收缩状态变化事件
  emit('collapse-change', isCollapse.value)
}

// 定义事件
const emit = defineEmits(['collapse-change'])
</script>

<template>
  <div class="nav-wrapper">
    <!-- Logo区域 -->
    <div class="logo-area">
      <span class="logo-text" :class="{ 'collapsed-text': isCollapse }">VitalLink</span>
      <el-icon class="collapse-icon" @click="toggleCollapse">
        <component :is="isCollapse ? ArrowRight : ArrowLeft" />
      </el-icon>
    </div>

    <el-scrollbar>
      <el-menu :default-openeds="['1', '2','3']" router :collapse="isCollapse">
        <!-- 仪表盘部分 -->
        <div class="menu-section">
          <div class="section-title" :class="{ 'collapsed-title': isCollapse }">仪表盘</div>
          <el-menu-item index="/doctor/overview">
            <el-icon><HomeFilled /></el-icon>
            <span>概览</span>
          </el-menu-item>
          <el-menu-item index="/doctor/profile">
            <el-icon><Avatar /></el-icon>
            <span>个人中心</span>
          </el-menu-item>
          <el-menu-item index="/doctor/analysis">
            <el-icon><PieChart /></el-icon>
            <span>数据分析</span>
          </el-menu-item>
          <el-menu-item index="/doctor/schedule">
            <el-icon><DataAnalysis /></el-icon>
            <span>日程</span>
          </el-menu-item>
        </div>

        <!-- 预约管理部分 -->
        <div class="menu-section">
          <div class="section-title" :class="{ 'collapsed-title': isCollapse }">预约管理</div>
          <el-sub-menu index="3">
            <template #title>
              <el-icon><Calendar /></el-icon>
              <span>预约服务</span>
            </template>
            <el-menu-item index="/doctor/appointment">
              <el-icon><Calendar /></el-icon>
              <span>预约管理</span>
            </el-menu-item>
          </el-sub-menu>
        </div>

        <!-- 患者管理部分 -->
        <div class="menu-section">
          <div class="section-title" :class="{ 'collapsed-title': isCollapse }">患者管理</div>
          <el-sub-menu index="1">
            <template #title>
              <el-icon><User /></el-icon>
              <span>患者服务</span>
            </template>
            <el-menu-item index="/doctor/patient-list">
              <el-icon><Calendar /></el-icon>
              <span>患者列表</span>
            </el-menu-item>
            <el-menu-item index="/doctor/patient-trace">
              <el-icon><Calendar /></el-icon>
              <span>患者跟进</span>
            </el-menu-item>
          </el-sub-menu>
        </div>



        <!-- 远程支持部分 -->
        <div class="menu-section">
          <div class="section-title" :class="{ 'collapsed-title': isCollapse }">远程支持</div>
          <el-sub-menu index="2">
            <template #title>
              <el-icon><ChatDotRound /></el-icon>
              <span>在线服务</span>
            </template>
            <el-menu-item index="/doctor/consultation">
              <el-icon><Bell /></el-icon>
              <span>在线问诊</span>
            </el-menu-item>
            <el-menu-item index="/doctor/emergency">
              <el-icon><FirstAidKit /></el-icon>
              <span>紧急医疗处理</span>
            </el-menu-item>
          </el-sub-menu>
        </div>
      </el-menu>
    </el-scrollbar>
  </div>
</template>

<style scoped lang="scss">
.nav-wrapper {
  height: 100%;
  display: flex;
  flex-direction: column;
  background-color: #fff;
  transition: all 0.3s;
}

.logo-area {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px;
  border-bottom: 1px solid #f0f0f0;
  margin-bottom: 10px;
  position: relative;
}

.collapse-icon {
  cursor: pointer;
  font-size: 18px;
  color: #27ba9b;
  transition: all 0.3s;
  &:hover {
    transform: scale(1.2);
  }
}

.collapsed-text {
  display: none;
}

.nav-logo {
  width: 24px;
  height: 24px;
  margin-right: 8px;
}

.logo-text {
  font-size: 18px;
  font-weight: 600;
  color: #27ba9b;
}

.menu-section {
  margin-bottom: 16px;
}

.section-title {
  padding: 8px 16px;
  font-size: 12px;
  color: #909399;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.el-menu {
  border-right: none;
  background-color: transparent;
  transition: width 0.3s;

  .el-menu-item, .el-sub-menu__title {
    height: 40px;
    line-height: 40px;
    border-radius: 4px;
    margin: 4px 8px;
    transition: all 0.3s;

    .el-icon {
      margin-right: 8px;
      font-size: 18px;
    }

    &:hover {
      background: linear-gradient(135deg, #8AF6C9 0%, #2BD18A 100%);
      color: #ffffff;
    }
  }

  .el-menu-item.is-active {
    background: linear-gradient(135deg, #2BD18A 0%, #1AB777 100%);
    color: #ffffff;
    font-weight: 500;
  }

  .el-sub-menu__title {
    font-weight: 500;
  }

  .el-sub-menu .el-menu-item {
    min-width: auto;
    height: 36px;
    line-height: 36px;
    padding-left: 50px !important;
  }
}

/* 隐藏滚动条 */
:deep(.el-scrollbar__bar) {
  display: none;
}

/* 收缩状态下的样式 */
.el-menu--collapse {
  width: 64px;

  .el-sub-menu__title span {
    display: none;
  }

  .el-menu-item {
    .el-icon {
      margin: 0;
    }
    span {
      display: none;
    }
  }
}

/* 收缩状态下隐藏小标题 */
.collapsed-title {
  display: none;
}

/* 确保过渡效果流畅 */
.el-menu--collapse .el-sub-menu__title .el-sub-menu__icon-arrow {
  display: none;
}
</style>
